package com.cy.ppj.web.admin.user;

import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.dao.mongodb.TeamCommissionSettingsLogDao;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.admin.TeamCommissionSettingsAO;
import com.cy.ppj.model.doc.TeamCommissionSettingsLog;
import com.cy.ppj.model.domian.admin.SystemSettings;
import com.cy.ppj.model.dto.sys.TeamCommissionSettingsDTO;
import com.cy.ppj.model.vo.admin.setting.TeamCommissionSettingsVO;
import com.cy.ppj.service.sys.SystemSettingsService;
import com.cy.ppj.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: 团队返佣设置
 * @author: Ocean
 * @create: 2018-09-05 14:52
 **/
@Slf4j
@RestController
@RequestMapping("/admin/team/commission")
@Api(tags = "后台-团队返佣设置")
public class AdminTeamCommissionController extends BaseController {

    @Autowired
    private SystemSettingsService systemSettingsService;
    @Autowired
    private TeamCommissionSettingsLogDao teamCommissionSettingsLogDao;

    @ApiOperation("团队返佣设置")
    @GetMapping("/params")
    public Result<List<TeamCommissionSettingsVO>> params() {
        TeamCommissionSettingsDTO teamCommissionSettings = systemSettingsService.queryTeamCommissionSettingsInCache();
        List<TeamCommissionSettingsVO> list = new ArrayList<>();
        // 一级
        TeamCommissionSettingsVO first = new TeamCommissionSettingsVO();
        first.setId(teamCommissionSettings.getFirstSettingId());
        first.setLevel(1);
        first.setRate(teamCommissionSettings.getFirstRate());
        first.setUpdateTime(teamCommissionSettings.getFirstUpdateTime());
        list.add(first);
        
        // 二级
        TeamCommissionSettingsVO second = new TeamCommissionSettingsVO();
        second.setId(teamCommissionSettings.getSecondSettingId());
        second.setLevel(2);
        second.setRate(teamCommissionSettings.getSecondRate());
        second.setUpdateTime(teamCommissionSettings.getSecondUpdateTime());
        list.add(second);
        
        
        return ok(null, list);
    }

    @ApiOperation("团队返佣设置编辑")
    @PostMapping("/edit")
    public Result edit(@Validated TeamCommissionSettingsAO teamCommissionSettingsAO, BindingResult bindingResult) {

        this.checkError(bindingResult);
        Date time = new Date();
        int level = 0;
        BigDecimal oldRate = BigDecimal.ZERO;
        TeamCommissionSettingsDTO teamCommissionSettings = systemSettingsService.queryTeamCommissionSettingsInCache();
        if (teamCommissionSettings.getFirstSettingId().equals(teamCommissionSettingsAO.getId())) {
            level = 1;
            oldRate = teamCommissionSettings.getFirstRate();
        } else if (teamCommissionSettings.getSecondSettingId().equals(teamCommissionSettingsAO.getId())) {
            level = 2;
            oldRate = teamCommissionSettings.getSecondRate();
        }

        if (level == 0) {
            return fail("数据异常");
        }

        SystemSettings systemSettings = new SystemSettings();
        systemSettings.setId(teamCommissionSettingsAO.getId());
        systemSettings.setValue(teamCommissionSettingsAO.getRate().toString());
        systemSettings.setUpdateTime(time);
        systemSettingsService.updateSystemSettings(systemSettings);

        TeamCommissionSettingsLog log = new TeamCommissionSettingsLog();
        log.setLevel(level);
        log.setOldRate(oldRate);
        log.setNewRate(teamCommissionSettingsAO.getRate());
        log.setCreateTime(time);
        teamCommissionSettingsLogDao.insert(log);

        return ok();
    }
    
    @ApiOperation("修改记录")
    @GetMapping("/edit/records")
    public PageBean<TeamCommissionSettingsLog> editRecords(int page, int limit) {
    	if(CommonUtils.isEmpty(page)) {
    		page = 1;
    	}
    	if(CommonUtils.isEmpty(limit)) {
    		limit = 10;
    	}
    	page = page - 1;
    	Pageable pageable = new PageRequest(page, limit, Sort.Direction.DESC, "createTime");
    	Page<TeamCommissionSettingsLog> pageModel = teamCommissionSettingsLogDao.findAll(pageable);
    	List<TeamCommissionSettingsLog> list = pageModel.getContent();
    	int total = (int) pageModel.getTotalElements();
    	return new PageBean<>(list, total);
    }
    
}
